﻿using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client
{
    public partial class ChooseForm : BaseForm
    {
        BLL.ServiceComm service = new BLL.ServiceComm();
        public ChooseForm()
        {
            InitializeComponent();
            BindExamPlace();
        }

        private void BindExamSpecNo()
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                return;
            }
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            using (DataTable dt = service.GetSpecialityKindsTableByExamPlaceID_CS(ExamPlaceID))
            {
                listSpeciality.DisplayMember = "SpecialityKindsName";
                listSpeciality.ValueMember = "SpecialityKindsNO";
                listSpeciality.DataSource = dt;
            }
        }

        private void BindExamPlace()
        {
            using (DataTable dt = service.GetExamPlaceTableALL_CS())
            {
                listExamPlace.DisplayMember = "ExamPlaceName";
                listExamPlace.ValueMember = "ExamPlaceNO";
                listExamPlace.DataSource = dt;
            }
        }

        private void BindExamLevel()
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                return;
            }
            if (listSpeciality.SelectedIndex < 0)
            {
                return;
            }
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            string SpecialityKindsNO = string.Empty;
            if (listSpeciality.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listSpeciality.SelectedItem;
                SpecialityKindsNO = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            using (DataTable dt = service.GetExamLevelByPlaceID_Speciality_CS(ExamPlaceID, SpecialityKindsNO))
            {
                //dt.Rows.Remove(dt.Select("DicNO='0'")[0]);
                listExamLevel.DisplayMember = "DicName";
                listExamLevel.ValueMember = "DicNO";
                listExamLevel.DataSource = dt;
            }
        }

        private void BindExamRoom()
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                return;
            }
            if (listSpeciality.SelectedIndex < 0)
            {
                return;
            }
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            string SpecialityKindsNO = string.Empty;
            if (listSpeciality.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listSpeciality.SelectedItem;
                SpecialityKindsNO = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            string ExamLevel = string.Empty;
            if (listExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamLevel.SelectedItem;
                ExamLevel = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            using (DataTable dt = service.GetExamRoomByPlaceID_Speciality_Level_CS(ExamPlaceID, SpecialityKindsNO, ExamLevel))
            {
                listExamRoom.DisplayMember = "ExamRoomName";
                listExamRoom.ValueMember = "ExamRoomNO";
                listExamRoom.DataSource = dt;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考点!");
                return;
            }
            if (listSpeciality.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择专业!");
                return;
            }
            if (listExamLevel.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择级别!");
                return;
            }
            if (listExamRoom.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考场!");
                return;
            }
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            string SpecialityKindsNO = string.Empty;
            string SpecialityKindsName = string.Empty;
            string ExamLevel = string.Empty;
            string ExamLevelName = string.Empty;
            string ExamRoom = string.Empty;
            string ExamRoomName = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            if (listSpeciality.SelectedItem != null)
            {
                if (listSpeciality.SelectedItems.Count > 1)
                {
                    DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listSpeciality.SelectedItems;
                    for (int i = 0; i < selectItems.Count; i++)
                    {
                        DataRowView dr = (DataRowView)selectItems[i];
                        if (SpecialityKindsNO.Length == 0)
                        {
                            SpecialityKindsNO = dr[0].ToSafeString();
                            SpecialityKindsName = dr[2].ToSafeString();
                        }
                        else
                        {
                            SpecialityKindsNO = SpecialityKindsNO + "," + dr[0].ToSafeString();
                            SpecialityKindsName = SpecialityKindsName + "," + dr[2].ToSafeString();
                        }
                    }
                }
                else
                {
                    DataRowView drExamLevel = (DataRowView)listSpeciality.SelectedItem;
                    SpecialityKindsNO = drExamLevel.Row.ItemArray[0].ToSafeString();
                    SpecialityKindsName = drExamLevel.Row.ItemArray[2].ToSafeString();
                }
            }
            if (listExamLevel.SelectedItem != null)
            {
                DataRowView drExamLevel = (DataRowView)listExamLevel.SelectedItem;
                ExamLevel = drExamLevel.Row.ItemArray[0].ToSafeString();
                ExamLevelName = drExamLevel.Row.ItemArray[1].ToSafeString();
            }
            if (listExamRoom.SelectedItem != null)
            {
                DataRowView drExamLevel = (DataRowView)listExamRoom.SelectedItem;
                ExamRoom = drExamLevel.Row.ItemArray[0].ToSafeString();
                ExamRoomName = drExamLevel.Row.ItemArray[2].ToSafeString();
            }

            SessionerHelper.ExaminationID = SessionerHelper.ExamLevel = SessionerHelper.ExamLevelName =
                SessionerHelper.ExamPlaceID = SessionerHelper.ExamPlaceName =
                SessionerHelper.ExamRoomID = SessionerHelper.ExamRoomName =
                SessionerHelper.SpecialityKinds = SessionerHelper.SpecialityKindsNO = string.Empty;

            SessionerHelper.SpecialityKinds = SpecialityKindsName;
            SessionerHelper.SpecialityKindsNO = SpecialityKindsNO;
            SessionerHelper.ExamLevel = ExamLevel;
            SessionerHelper.ExamLevelName = ExamLevelName;
            SessionerHelper.ExamPlaceID = ExamPlaceID;
            SessionerHelper.ExamPlaceName = ExamPlaceName;
            SessionerHelper.ExamRoomID = ExamRoom;
            SessionerHelper.ExamRoomName = ExamRoomName;
            TB_Exam_Entretenimientoh model = service.GetEntretenimientohModel(ExamLevel, ExamPlaceID, SpecialityKindsNO);
            if (model != null)
            {
                SessionerHelper.ExaminationID = model.EntretenimientohID;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void listExamPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExamSpecNo();
        }

        private void listSpeciality_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExamLevel();
        }

        private void listExamLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExamRoom();
        }
    }
}
